package database;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.TextArea;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;


public class MainWin {
    
    private static final String DB_SERVER = "xtremeduty.com:3306";
    private static final String USERNAME = "recipes";
    private static final String PASSWORD = "recipes";
    private static final String SCHEMA = "recipes";
    private JFrame frame;
    private JTextField txtDbServer;
    private JTextField txtDatabase;
    private TextArea txtQuery;
    private TextArea txtResult;
    private JLabel lblUsername;
    private JTextField txtUsername;
    private JLabel lblPassword;
    private JPasswordField txtPwd;
    private Button button_1;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWin window = new MainWin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MainWin() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 855, 541);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        txtQuery = new TextArea();
        txtQuery.setText("Type Sql:");
        txtQuery.setBounds(29, 90, 602, 135);
        frame.getContentPane().add(txtQuery);
        
        txtResult = new TextArea();
        txtResult.setText("Result:");
        txtResult.setBounds(29, 242, 602, 196);
        frame.getContentPane().add(txtResult);
        
        JLabel lblDbServer = new JLabel("Db Server:");
        lblDbServer.setBounds(29, 22, 92, 14);
        frame.getContentPane().add(lblDbServer);
        
        txtDbServer = new JTextField();
        txtDbServer.setText(DB_SERVER);
        txtDbServer.setBounds(96, 19, 162, 20);
        frame.getContentPane().add(txtDbServer);
        txtDbServer.setColumns(10);
        
        JLabel lblDatabase = new JLabel("Database:");
        lblDatabase.setBounds(291, 22, 70, 14);
        frame.getContentPane().add(lblDatabase);
        
        txtDatabase = new JTextField();
        txtDatabase.setText(SCHEMA);
        txtDatabase.setColumns(10);
        txtDatabase.setBounds(357, 19, 162, 20);
        frame.getContentPane().add(txtDatabase);
        
        lblUsername = new JLabel("Username:");
        lblUsername.setBounds(29, 47, 92, 14);
        frame.getContentPane().add(lblUsername);
        
        txtUsername = new JTextField();
        txtUsername.setText(USERNAME);
        txtUsername.setColumns(10);
        txtUsername.setBounds(96, 47, 162, 20);
        frame.getContentPane().add(txtUsername);
        
        lblPassword = new JLabel("Password:");
        lblPassword.setBounds(291, 47, 70, 14);
        frame.getContentPane().add(lblPassword);
        
        txtPwd = new JPasswordField();
        txtPwd.setText(PASSWORD);
        txtPwd.setBounds(357, 47, 162, 20);
        frame.getContentPane().add(txtPwd);
        
        button_1 = new Button("Execute");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                execute();
            }
        });
        button_1.setBounds(648, 92, 117, 22);
        frame.getContentPane().add(button_1);
    }
    
    @SuppressWarnings("deprecation")
	private void execute() {
        Db db = Db.getDb();
        String result = null;
        try {
            result = db.executeQuery(txtDbServer.getText(), txtDatabase.getText(), txtQuery.getText(), txtUsername.getText(), txtPwd.getText());
        }
        catch (Exception ex) {
            if (ex.getMessage().equals("Can not issue data manipulation statements with executeQuery().")) {
                try {
                    result = db.executeNonQuery(txtDbServer.getText(), txtDatabase.getText(), txtQuery.getText(), txtUsername.getText(), txtPwd.getText());
                }
                catch (Exception ex2) {
                    result = ex2.getMessage();
                }
            }
            else {
            result = ex.getMessage();
            }
        }
        
        txtResult.setText(result);
    }   
}